package com.sos.logging
{
	import com.sos.Shared;
	import com.sos.utils.DateUtils;
	
	public class TextAreaLogTarget extends LogTarget
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		public static const		DEFAUT_MAX_LINES:int	= 100;
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private static var _textAreas:Array;
		
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function TextAreaLogTarget(options:LogTargetParams)
		{
			// Ancestor constructor
			super(LogTargetType.TEXTAREA, options);
			// Singleton check
			if (_textAreas != null) throw new Error("Only one instance of TextAreaLogTarget can be created!");
			// Init objects
			_textAreas = new Array();
		}

		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get count():int
		{
			return _textAreas.length;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------	
		
		protected function writeIntoTextArea(textArea:Object, maxLines:int, lines:Array):void
		{
			// 
			var logLines:Array = textArea.text.split("\r\n"); 
			// Check log lines and removes all lines after max number of lines
			if (logLines.length + lines.length >= maxLines)
			{
				logLines.splice(maxLines - lines.length);
			}
			// Add all lines
			for (var i:int = lines.length-1; i>=0; i--)
			{
				// var log:String = DateUtils.timestamp() + " | " + message;
				// var log:String = DateUtils.format(new Date(), "JJ:NN:SS") + " | " + lines[i];
				var log:String = lines[i];
				// Adds the new messag on top of the log
				logLines.splice(0, 0, log);
			}
			// Sets the text of the text area
			textArea.text = logLines.join("\r\n");
		}
		
		override protected function write(message:String):void
		{
			var lines:Array = new Array();
			// Check multltiline message
			if (isMultiline(message))
			{
				lines = split(message);
			}
			else
			{
				lines = [ message ];
			}
			// Loop over all registered text areas
			for (var i:int = 0; i < _textAreas.length; i++)
			{
				// retrieve the configuration
				var config:Object = _textAreas[i];
				// Check enabling
				if (config.enabled && config.textArea != null)
				{
					writeIntoTextArea(config.textArea, config.maxLines, lines);
				}
			}

		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------	
		
		protected static function indexOf(key:String):int
		{
			for (var i:int = 0; i < _textAreas.length; i++)
			{
				if (_textAreas[i].key == key) return i;
			}
			return Shared.INVALID_INDEX;
		}
		
		public static function enable(key:String):void
		{
			var index:int = indexOf(key);
			if (index != Shared.INVALID_INDEX)
			{
				_textAreas[index].enabled = true;
			}
			else throw new Error("TextArea configuration for key '" + key + "' doesn't exitst");
		}
		
		public static function disable(key:String):void
		{
			var index:int = indexOf(key);
			if (index != Shared.INVALID_INDEX)
			{
				_textAreas[index].enabled = false;
			}
			else throw new Error("TextArea configuration for key '" + key + "' doesn't exitst");
		}
		
		public static function exists(key:String):Boolean
		{
			return indexOf(key) != Shared.INVALID_INDEX;
		}
		
		public static function register(key:String, text:Object, maxlines:int = DEFAUT_MAX_LINES):void
		{
			var index:int = indexOf(key);
			if (index == Shared.INVALID_INDEX)
			{
				_textAreas.push({	key:		key,
									enabled:	true,
									textArea:	text,
									maxLines:	maxlines
								});
			}
			else throw new Error("TextArea configuration for key '" + key + "' already exitst");
		}
		
		public static function unregister(key:String):void
		{
			var index:int = indexOf(key);
			if (index != Shared.INVALID_INDEX)
			{
				_textAreas.splice(index, 1);
			}
			else throw new Error("TextArea configuration for key '" + key + "' doesn't exitst");
		}
		
	}
}